package com.steel.yfmall.cloud.assistant.user.impl;


import com.alibaba.fastjson.JSON;
import com.steel.yfmall.cloud.assistant.dto.para.user.ParaUserBaseBean;
import com.steel.yfmall.cloud.assistant.user.UserBaseService;
import com.steel.yfmall.cloud.components.utils.base.APIMsgBean;
import com.steel.yfmall.cloud.components.utils.utils.MobileBaseBeanUtils;
import com.steel.yfmall.cloud.user.api.bean.param.APIParaUserBase;
import com.steel.yfmall.cloud.user.api.bean.param.APIParaUserCompany;
import com.steel.yfmall.cloud.user.api.bean.response.APIResultUserBase;
import com.steel.yfmall.cloud.user.api.service.UserBaseServiceAPI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @program: UserBaseServiceImpl
 * @author: liuzhijun
 * @date: 2021-03-21 15:17
 **/
@Service
@Slf4j
public class UserBaseServiceImpl implements UserBaseService {


    @Override
    public APIMsgBean<Boolean> userSyncStatus(ParaUserBaseBean paraUserBaseBean) {
        log.info("接受苍穹推送用户信息: ", JSON.toJSONString(paraUserBaseBean));
        APIMsgBean<Boolean> booleanAPIMsgBean = new APIMsgBean<>();
        if (paraUserBaseBean != null && paraUserBaseBean.getStatus() == null) {
            booleanAPIMsgBean.setData(false);
            booleanAPIMsgBean.getSoaStateBean().setMessage("参数异常，状态不能为空");
            return booleanAPIMsgBean;
        }
        APIParaUserBase apiParaUserBase = new APIParaUserBase();
        APIParaUserCompany apiParaUserCompany = new APIParaUserCompany();
        MobileBaseBeanUtils.copyAllFields(paraUserBaseBean, apiParaUserBase);
        MobileBaseBeanUtils.copyAllFields(paraUserBaseBean, apiParaUserCompany);
        apiParaUserBase.setFlag(1);
        apiParaUserBase.setApiParaUserCompany(apiParaUserCompany);
        APIMsgBean<APIResultUserBase> apiMsgBean = userBaseServiceAPI.getUserByUserName(apiParaUserBase);
        if (apiMsgBean.getData() != null && apiMsgBean.getSoaStateBean().getCode() == 1) {
            booleanAPIMsgBean = userBaseServiceAPI.updateUserInfo(apiParaUserBase);
        } else {
            booleanAPIMsgBean = userBaseServiceAPI.insertUserBean(apiParaUserBase);
        }
        return booleanAPIMsgBean;
    }

    @Resource
    private UserBaseServiceAPI userBaseServiceAPI;
}
